import {MyFieldVue} from "../field/field";

import VDistpicker from '../../vue/v-distpicker/index'
import {MyTrigger} from "../trigger/trigger";

let context
var modalb = null
document.addEventListener('on-address-add', function () {
  context.model = context.getReset()
  // modalb.data('item', {})
  modalb.modal({
    backdrop: 'static'
  })
})
document.addEventListener('on-address-edit', function (e) {
  console.log('e', e)
  context.model = context.getReset(e.detail.item)
  // modalb.data('item', e.detail.item)
  modalb.modal({
    backdrop: 'static'
  })
})

export let MyAddress = {
  tagName: 'u-address',
  template: '#' + 'address-tpl',
  components: {
    'my-field': MyFieldVue,
    'v-distpicker': VDistpicker,
    [MyTrigger.tagName]: MyTrigger,
  },
  data() {
    return {
      model: {
        name: '',
        mobile: '',
        address: '',
        addressprovince: '',
        addresscity: '',
        addressdist: '',
        is_default: true
      },
      placeholders: {
        province: '请选择省份',
        city: '请选择城市',
        area: '请选择地区',
      }
    }
  },
  mounted() {
    context = this
    modalb = $('#modalb')
    // modalb.modal({
    //   backdrop: 'static'
    // })
  },
  methods: {
    getCls() {
      return {
        'placeholder-province': this.model && this.model.addressprovince === '',
        'placeholder-city': this.model && this.model.addresscity === '',
        'placeholder-area': this.model && this.model.addressdist === '',
      }
    },
    getReset(item = {}) {
      return {
        name: '',
        mobile: '',
        address: '',
        addressprovince: '',
        addresscity: '',
        addressdist: '',
        is_default: false,
        ...item
      }
    },
    onSelected(data) {
      this.model.addressprovince = data.province.value
      this.model.addresscity = data.city.value
      this.model.addressdist = data.area.value
    },
    initval({DISTRICTS}) {
      // console.log('initval', DISTRICTS['100000']['110000'])
      // DISTRICTS['100000']['110000'] =  DISTRICTS['100000']['110000'] + '!'
      return {
        DISTRICTS
      }
    },
    cancel() {
      $('#modalb').modal('hide')
    //
    },
    save() {
      $('#modalb').modal('hide')
      // console.log('sdsds')
      // document.dispatchEvent(new CustomEvent('on-address-save'))
    }
  }
}
